﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Drawing;

    public class Level : LinearControl
    {
        private ColorSection[] activeColorSection = new ColorSection[] { new ColorSection(Color.Lime, 50M), new ColorSection(Color.Yellow, 80M), new ColorSection(Color.Red, 100M) };
        private int divisions = 10;
        private ColorSection[] inactiveColorSection = new ColorSection[] { new ColorSection(Color.DarkGreen, 50M), new ColorSection(Color.Olive, 80M), new ColorSection(Color.Firebrick, 100M) };
        private int space = 1;

        protected override void DrawDynamic(Graphics g)
        {
            this.DrawLevel(g, this.ActiveColorSection, base.Round(base.ValueToPercent(base.Value) * this.Divisions));
            base.DrawDynamic(g);
        }

        private void DrawLevel(Graphics g, ColorSection[] color, int count)
        {
            int num = base.ReverseScale ? -1 : 1;
            Vector vector = new Vector(this.WorkRectangle.X, this.WorkRectangle.Y);
            Vector vector2 = new Vector(this.WorkRectangle.Width, this.WorkRectangle.Height);
            Vector vector3 = new Vector(base.Direction, (int) (num * ((((vector2[base.Direction] - (2 * base.Indent)) + this.Space) / this.Divisions) - this.Space)), base.BarWidth);
            int num2 = (int) (((vector2[base.Direction] - (2 * base.Indent)) + this.Space) - (((num * vector3[base.Direction]) + this.Space) * this.Divisions));
            Vector vector4 = new Vector(base.Direction, ((vector[base.Direction] + (base.Indent * num)) + (base.ReverseScale ? vector2[base.Direction] : 0M)) - num, base.CenterLine - (base.BarWidth / 2M));
            for (int i = 0; i < count; i++)
            {
                VectorRectangle rectangle;
                Vector vector5 = new Vector(base.Direction, vector4[base.Direction] + (i * (vector3[base.Direction] + (num * this.Space))), vector4.GetSecondaryValue(base.Direction));
                if (i > num2)
                {
                    rectangle = new VectorRectangle(new Vector(base.Direction, vector5[base.Direction] + (num * (num2 + 2)), vector5.GetSecondaryValue(base.Direction)), new Vector(base.Direction, vector3[base.Direction], vector3.GetSecondaryValue(base.Direction)));
                }
                else
                {
                    rectangle = new VectorRectangle(new Vector(base.Direction, vector5[base.Direction] + (num * (i + 1)), vector5.GetSecondaryValue(base.Direction)), new Vector(base.Direction, vector3[base.Direction] + num, vector3.GetSecondaryValue(base.Direction)));
                }
                g.FillRectangle(new SolidBrush(this.GetColor(((i + 1) / this.Divisions) * 100M, color)), rectangle.PositiveRectangle().Round());
            }
        }

        protected override void DrawStatic(Graphics g)
        {
            base.DrawStatic(g);
            this.DrawLevel(g, this.InactiveColorSection, this.Divisions);
        }

        private Color GetColor(decimal curentValue, ColorSection[] color)
        {
            foreach (ColorSection section in color)
            {
                if (section.MaximumPercent >= curentValue)
                {
                    return section.Color;
                }
            }
            return Color.Black;
        }

        public ColorSection[] ActiveColorSection
        {
            get
            {
                return this.activeColorSection;
            }
            set
            {
                if ((value == null) || (value.Length == 0))
                {
                    value = new ColorSection[] { new ColorSection(Color.Black, 100M) };
                }
                this.activeColorSection = value;
                base.InvalidateStatic();
            }
        }

        protected override Size DefaultControlSize
        {
            get
            {
                return new Size(200, 100);
            }
        }

        public int Divisions
        {
            get
            {
                return this.divisions;
            }
            set
            {
                if (this.divisions != value)
                {
                    this.divisions = value;
                    base.InvalidateStatic();
                }
            }
        }

        public ColorSection[] InactiveColorSection
        {
            get
            {
                return this.inactiveColorSection;
            }
            set
            {
                if ((value == null) || (value.Length == 0))
                {
                    value = new ColorSection[] { new ColorSection(Color.Black, 100M) };
                }
                this.inactiveColorSection = value;
                base.InvalidateStatic();
            }
        }

        public int Space
        {
            get
            {
                return this.space;
            }
            set
            {
                if ((this.space != value) && (this.space >= 0))
                {
                    this.space = value;
                    base.InvalidateStatic();
                }
            }
        }
    }
}

